<?php
if($_SESSION['jadwalsekarang']!=1){
	echo "Akses tidak diizinkan";
	exit();
}

$act = filter($_GET['act']);
$field = filter($_GET['field']);
$q = filter($_GET['q']);

if(empty($act)){	
?>		
					<!-- box / title -->
					<div class="title">
						<h5>Jadwal Hari Ini</h5>								
						<div class="search global-nav">
							<form method="GET" class="form-search js-search-form" action="index.php?pages=jadwal" id="global-nav-search" style="width:350px">
								<input type="hidden" name="pages" value="jadwal">
								<select name="field" id="birthday_month" aria-label="Month" class="" >
									<option value="" <?php echo ($field=="")?"selected":"";?>>Cari Berdasarkan:</option>
									<option value="tgl_jual" <?php echo ($field=="tgl_jual")?"selected":"";?>>Tanggal</option>
									<option value="b.nama" <?php echo ($field=="b.nama")?"selected":"";?>>Seles</option>
									<option value="c.nama" <?php echo ($field=="c.nama")?"selected":"";?>>Customer</option>
									<option value="remainder" <?php echo ($field=="remainder")?"selected":"";?>>Remainder</option>									
								</select>

				            	<label class="visuallyhidden" for="search-query">Search query</label>
				            	<input class="search-input" type="text" id="search-query" placeholder="Search" name="q" value="<?php echo $q;?>">
				            	<span class="search-icon js-search-action">
				              		<button type="submit" class="icon nav-search" tabindex="-1">
				              			<span class="visuallyhidden">Search</span>
				              		</button>
				            	</span>
				            	<input disabled="disabled" class="search-input search-hinting-input" type="text" id="search-query-hint" autocomplete="off" spellcheck="false">				              					              
							</form>
						</div>
					</div>
					<!-- end box / title -->
					
					<div class="table">										
						<table>
							<thead>
								<tr>
									<th class="action">No.</th>
									<th class="left">Tanggal Kunjungan</th>
									<th class="left">Sales</th>
									<th class="left">Telpon Sales</th>
									<th class="left">Customer</th>
									<th>Alamat</th>
									<th>Telpon</th>
								</tr>
							</thead>
							<tbody>
							<?php			

							$q2=$q;
							if($field=='status'){
								$q2 = (strtolower($q)=='aktif')?'1':'0';
							}
							
							$addQuery='';


							if(!empty($field))
								$addQuery .= " WHERE $field LIKE '%$q2%' ";
							
							if(!empty($idsales))
								$addQuery .= " AND b.id_sales = '$idsales' ";

							$batas=10;
							$pg=(int) $_GET['pg'];
							if(empty($pg))
							{
								$posisi=0;
								$pg=1;
							}
							else
							{
								$posisi = ($pg-1) * $batas;
							}
							$date = date('Y-m-d');
							$sqlKode1 = mysql_query("SELECT tgl_jual, b.nama as nama_sales, c.nama as nama_customer, c.alamat as alamat, b.telp as telp_sales, c.telp as telp_customer  FROM tb_schedule a
INNER JOIN tb_sales b
ON a.id_sales = b.id_sales
INNER JOIN tb_customer c
ON a.id_customer=c.id_customer AND a.tgl_jual = '$date' $addQuery ORDER BY tgl_jual DESC limit $posisi, $batas");
							
							$no=$posisi+1;
							while($dataKode = mysql_fetch_array($sqlKode1)){								

								$status = ($dataKode['status']==1)? "Aktif":"Non Aktif";
							?>
								<tr>
									<td class="center"><?php echo $no;?></td>
									<td><?php echo $dataKode['tgl_jual'];?></td>
									<td><?php echo $dataKode['nama_sales'];?></td>
									<td><?php echo $dataKode['telp_sales'];?></td>
									<td><?php echo $dataKode['nama_customer'];?></td>
									<td><?php echo $dataKode['alamat'];?></td>
									<td><?php echo $dataKode['telp_customer'];?></td>							
								</tr>
							<?php	
								$no++;				
							}

							$sqlSup2 = mysql_query("SELECT tgl_jual, b.nama as nama_sales, c.nama as nama_customer, c.alamat as alamat, b.telp as telp_sales, c.telp as telp_customer  FROM tb_schedule a
INNER JOIN tb_sales b
ON a.id_sales = b.id_sales
INNER JOIN tb_customer c
ON a.id_customer=c.id_customer AND a.tgl_jual = '$date' $addQuery ORDER BY tgl_jual DESC");
							$jumlahData = mysql_num_rows($sqlSup2);

							$file="?pages=jadwal_sekarang&field=$field&q=$q";
							$jmlhalaman=ceil($jumlahData/$batas);
	
							?>															
							</tbody>
						</table>
						<!-- pagination -->
						<div class="pagination pagination-left">
							<div class="results">
								<span><?php echo "Showing Results ".($posisi+1)."-".($no-1)." of $jumlahData";?></span>
							</div>
							<ul class="pager">
								<?php
								//link ke halaman sebelumnya (previous)
								if($pg > 1){
									$previous=$pg-1;
									echo "<li><a href=$file&pg=1>&laquo; First</a></li>
									<li><a href=$file&pg=$previous>&laquo; Prev</a></li> ";
								}else{ 
									echo "<li class='disabled'>&laquo; First</li>
									<li class='disabled'>&laquo; Prev</li> ";
								}
								?>

								<?php
								//Link untuk Angka
								$angka=($pg > 3 ? " <li class='separator'>...</li> " : " ");
								for($i=$pg-2;$i<$pg;$i++)
								{
								  if ($i < 1) 
								      continue;
								  $angka .= "<li><a href='$file&pg=$i'>$i</a></li> ";
								}

								$angka .= " <li class='current'>$pg</li>";
								for($i=$pg+1;$i<($pg+3);$i++)
								{
								  if ($i > $jmlhalaman) 
								      break;
								  $angka .= "<li><a href='$file&pg=$i'>$i</a></li> ";
								}

								$angka .= ($pg+2<$jmlhalaman ? " <li class='separator'>...</li> <li><a href='$file&pg=$jmlhalaman'>$jmlhalaman</a></li>" : " ");

								echo "$angka";
								?>
								

								<?php
								//link kehalaman berikutnya (Next)
								if($pg < $jmlhalaman)
								{
									$next=$pg+1;
									echo "<li><a href='$file&pg=$next'>Next &raquo;</a></li>
									<li><a href='$file&pg=$jmlhalaman'>Last &raquo;</a></li>";
								}
								else
								{ 
									echo "<li class='disabled'>Next &raquo;</li>
									<li class='disabled'>Last &raquo;</li> ";
								}		
								?>													
							</ul>
						</div>

						<!-- end pagination -->
						<!-- table action -->
						
						<!-- end table action -->						
					</div>

<?php
}elseif ($act=='edit') {

	$id_kode = (int) filter($_GET['id']);

	$sqlKode = mysql_query("SELECT * FROM tb_schedule a,tb_customer b WHERE a.id_customer=b.id_customer AND id_schedule=$id_kode");
	$dataKode = mysql_fetch_array($sqlKode);

	if(!empty($id_kode)){
		?>
		<!-- box / title -->
		<div class="title">
			<h5>Ubah Jadwal Kunjungan </h5>														
		</div>
		<!-- end box / title -->
		<?php
	}else{
		?>
		<!-- box / title -->
		<div class="title">
			<h5>Tambah Jadwal Kunjungan</h5>														
		</div>
		<!-- end box / title -->
		<?php
	}

?>	

	<div class="table">
		<form method="POST" action="?pages=jadwal&act=update">
		<input type="hidden" name="id_kode" value="<?php echo $dataKode['id_schedule'];?>">
		<table style="width:auto">
		   	<tbody>
		   		
				<tr>
					<td style="vertical-align: top;"><label>Nama Customer</label></td>
					<td>:</td>
					<td>
						<select name="id_customer" id="id_customer">
						<option value="">PILIH Customer</option>
					<?php 
					$sqlBB = mysql_query("SELECT * FROM tb_customer");
					while($dataBB = mysql_fetch_array($sqlBB)){
						if($dataBB['id_customer']==$dataKode['id_customer']){
							$selected = " selected ";
						}else{
							$selected = "";
						}
						echo '<option value="'.$dataBB['id_customer'].'" '.$selected.'>'.$dataBB['nama'].'</option>';
					}
					?>
					</select><input type="hidden" name="nama_bb" id="nama_bb" />
			   	</tr>
				<tr>
					<td><label>Kunjungan Terakhir</label></td>
					<td>:</td>
					<td><input type="text" value="<?php echo $dataKode['kode_bb'];?>" size="30" name="kunjungan" id="kunjungan" placeholder="Tanggal Kunjungan" readonly="true"></td>
			   	</tr>
				<tr>
					<td><label>Remainder</label></td>
					<td>:</td>
					<td><input type="text" value="<?php echo $dataKode['remainder'];?>" size="30" name="remainder" id="remainder" placeholder="Remainder" readonly="true"></td>
			   	</tr>
				<tr>
					<td><label>Tanggal Kunjungan</label></td>
					<td>:</td>
					<td><input type="text" value="<?php echo $dataKode['tgl_jual'];?>" size="60" name="tgl_kunjungan" id="tgl_kunjungan" placeholder="Tanggal" required></td>
			   	</tr>
				<tr>
					<td><label>Nama Sales</label></td>
					<td>:</td>
					<td>
                    <select class="select" style="width: 210px;" name="id_sales" id="id_sales" placeholder="Nama Suplier" required>
                      <option >- PILIH Sales -</option>
                      
                      <?php
						$select_jur = mysql_query("SELECT * FROM tb_sales");
						while($select_jur2 = mysql_fetch_array($select_jur)){
							if($select_jur2['id_sales']==$dataKode['id_sales']){
								$selected = " selected ";
							}else{
								$selected = "";
							}
                      ?>
                      <option <?php echo $selected;?> value="<?php echo"$select_jur2[id_sales]";?>"><?php echo"$select_jur2[nama]";?></option>
                      <?php
                            }
                      ?>
                        </select>
			   	</tr>
			 	<tr>
			 		<td>&nbsp;</td>
			 		<td>&nbsp;</td>
			 		<td>
			 			<input type="hidden" value="" size="15" name="id">
						<input type="submit" value="Simpan" name="submit" class="btn btn-primary">
						<input type="Reset" value="Reset" class="btn">			 		</td>
		   		</tr>
		 	</tbody>
		</table>
		</form>
	</div>
<?php
}elseif ($act=='update') {
	# code...
	$id_kode = (int) $_POST['id_kode'];
	$id_sales = $_POST['id_sales'];
	$id_customer = $_POST['id_customer'];
	$tgl_kunjungan = $_POST['tgl_kunjungan'];
	

	if(!empty($id_kode)){
		$update = mysql_query("UPDATE tb_schedule SET id_sales='$id_sales', id_customer='$id_customer', tgl_jual='$tgl_kunjungan' WHERE id_schedule=$id_kode");
	
		if($update)
			sukses('Data berhasil disimpan', '?pages=jadwal');
		else
			sukses('Data gagal disimpan', '?pages=jadwal');
			
	}else{
		$insert = mysql_query("INSERT INTO tb_schedule VALUES(NULL, '$id_sales', '$id_customer', '$tgl_kunjungan' )");

		if($insert)
			sukses('Data berhasil disimpan', '?pages=jadwal');
		else
			sukses('Data gagal disimpan', '?pages=jadwal');
	}
	
}elseif ($act=='del') {
	# code...
	$id_kode = (int) filter($_GET['id']);

	$delete = mysql_query("DELETE FROM tb_schedule WHERE id_schedule=$id_kode");

	if($delete)
		sukses('Data berhasil dihapus', '?pages=jadwal');
}
?>					